﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Common_FindBook : System.Web.UI.Page
{

    private static string  recordPara;
    public static string para;
    public string navstring,data;
    public string  para2, para3, para4,para9;
    public string para5, para6, para7;
    public string url2, url4, url3;//这三个参数用来传参，因为para已经加上了其他变量，导致url不带参数了
    public int countNum = 0;
    public string suoyin;
    public string display1, display2, display3;
    public string companynum, writenum, publishdatenum;
    public string orderCon, orderOpt;
    public static bool flag2=false;
    protected void Page_Load(object sender, EventArgs e)
    {

        display1 = "block";
        display2 = "block";
        display3 = "block";

        //排序条件和语句：
        if (DropDownList1.SelectedItem.ToString().Equals("按出版社排序"))
        {
            orderCon = "order by PublishCompanyName";
        }
        else
        {
            orderCon = " order by PublishDate";
        }

        if (DropDownList2.SelectedItem.ToString().Equals("升序排列"))
        {
            orderOpt = " asc";
        }
        else
        {
            orderOpt = " DESC";
        }

        if (!IsPostBack)
        {
            flag2 = true;
            if (Session["userName"] == null)
            {
                navstring =  "<a style=\"width: 84px;color:#fff;\" href=\"#\" data-toggle=\"modal\" data-target=\"#myModal\"><span  class=\"glyphicon glyphicon-log-in\"></span> 登录";
            }
            else
            {
                navstring = "<a href=\"../User/UserIndexInfo.aspx\" style=\"width: 84px;color:#fff;\" ><span  class=\"glyphicon glyphicon-user\"></span>" + Session["userName"].ToString();
            }



            if (Request.QueryString["bookName"] != null)
                 para = Request.QueryString["bookName"];//获取参数

            if (Request.QueryString["publishCompany"] != null)
            {
                para5 = "、" + Request.QueryString["publishCompany"];
                display1 = "none";
                url2 = "&publishCompany=" + Request.QueryString["publishCompany"];
                para2 = "and PublishCompanyName='" + Request.QueryString["publishCompany"] + "'";
            }
            if (Request.QueryString["publishDate"] != null)
            {
                display3 = "none";
                url3 = "&PublishDate=" + Request.QueryString["publishDate"];
                para6 = "、" + string.Format("{0:yyyy-MM}", DateTime.Parse(Request.QueryString["publishDate"])) + "、";
                para3 = "and PublishDate='" + Request.QueryString["publishDate"] + "'";
            }
            if (Request.QueryString["Writer"] != null)
            {

                display2 = "none";
                url4 = "&Writer=" + Request.QueryString["Writer"];
                para7 = "、" + Request.QueryString["Writer"] + "、";
                para4 = "and Writer='" + Request.QueryString["Writer"] + "'";

            }
            recordPara = para2 + para3 + para4;
          
            
            if (para!=null)
            {

                setdata("select distinct BookName,Writer,book_info.PublishCompanyID,PublishDate,ISBN,FindBookNum,Price,Amount,CurrentNum,InDate,BeBorrowTimes,BookPlace,Publish_company.PublishCompanyName,Book_Type.TypeName,BookID  ,imageurl from  Book_info,Publish_company,Book_Type  where book_info.PublishCompanyID=Publish_company.PublishCompanyID  and ( BookName like '%" + para + "%' or Writer like '%" + para + "%' )and Book_info.BookType=Book_Type.BookType " + para2 + para3 + para4 + orderCon + orderOpt + "", para);
                
            }
            
            else 
            {
                Response.Write("<script language=javascript>alert('请输入关键字');</script>");
            }




        }
        if (para != null && flag2 == false && TBRESULT.Text=="")
        {

            setdata("select distinct BookName,Writer,book_info.PublishCompanyID,PublishDate,ISBN,FindBookNum,Price,Amount,CurrentNum,InDate,BeBorrowTimes,BookPlace,Publish_company.PublishCompanyName,Book_Type.TypeName,BookID ,imageurl from  Book_info,Publish_company,Book_Type  where book_info.PublishCompanyID=Publish_company.PublishCompanyID  and ( BookName like '%" + para + "%' or Writer like '%" + para + "%' )and Book_info.BookType=Book_Type.BookType " + para2 + para3 + para4 + orderCon + orderOpt + "", para);
        }

        flag2 = false;
       
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string bookName = "";
        string url = "";
        bookName = TBFindBook.Value.ToString();
        url = "FindBook.aspx?bookName=" + bookName;
        Response.Redirect(url);
    }

    protected void setdata(string EXSQL,string para)
    {
        SQL sql = new SQL();
        DataSet ds = sql.getDataset(EXSQL, para);
        DataTable dt = new DataTable();
        DataRow drow;
        DataRowCollection drc;
        dt = ds.Tables[0];
        drc = dt.Rows;
        for (int i = 0; i < drc.Count; i++)
        {
            drow = drc[i];
            string bookname = drow[0].ToString();
            string write = drow[1].ToString();
            string publishdate =drow[3].ToString();
            publishdate = string.Format("{0:yyyy-MM-dd}", DateTime.Parse(publishdate));
            string isbn = drow[4].ToString();
            string findbooknum = drow[5].ToString();
            string price = drow[6].ToString();
            string amount = drow[7].ToString();
            string nownum = drow[8].ToString();
            string indate = string.Format("{0:yyyy-MM-dd}", DateTime.Parse(drow[9].ToString()));
            string beborrow = drow[10].ToString();
            string bookplace = drow[11].ToString();
            string publishCompany = drow[12].ToString();
            
            string bookTypename = drow[13].ToString();
            string bookID = drow[14].ToString();
            string imageURL = drow[15].ToString();

            data += "<li class=\"span3\" style=\"float: left; text-align: justify; width: 722px;\"><div class=\"thumbnail\"><div class=\"caption\" style=\"overflow:auto\"><div style=\"float:left;width:94px;height:134px;border: 2px solid #cccfd9;\"><img src=\""+imageURL+"\" ></div><div style=\"float:left;width:575px;margin-left:20px;\"><p style=\"margin-bottom: 5px;padding-bottom: 5px;border-bottom: 1px dashed #b7b7b7;color: #a1973b;font-size: 18px;text-align:left;font-family:FZBeiWeiKaiShu-S19S\"><B>" + bookname + "</B></p><div class=\"titbar\"><span class=\"author\">作者：<strong>" + write + "</strong></span>&nbsp;&nbsp;&nbsp;<span class=\"publisher\">出版社：<strong>" + publishCompany + "</strong></span>&nbsp;&nbsp;&nbsp;<span class=\"dates\">出版时间：<strong>" + publishdate + "</strong></span>&nbsp;&nbsp;&nbsp;<span class=\"dates\">ISBN：<strong>" + isbn + "</strong></span>&nbsp;&nbsp;&nbsp;<br /><br /><span class=\"dates\">索书号：<strong>" + findbooknum + "</strong></span>&nbsp;&nbsp;&nbsp;<span class=\"dates\">入库时间：<strong>" + indate + "</strong></span>&nbsp;&nbsp;&nbsp;<span class=\"dates\">图书总数：<strong>" + amount + "</strong></span>&nbsp;&nbsp;&nbsp;<span class=\"dates\">价格：<strong>" + price + "</strong></span>&nbsp;&nbsp;&nbsp;<br /><br /><span class=\"dates\">图书分类：<strong id=\"hldallnum2\">" + bookTypename + "</strong></span> &nbsp;&nbsp;&nbsp;<span class=\"dates\">在馆数：<strong id=\"hldcount2\">" + nownum + "</strong></span>&nbsp;&nbsp;&nbsp;<span class=\"dates\">累借天数：<strong id=\"loanDatanum2\">" + nownum + "</strong></span>&nbsp;&nbsp;&nbsp; <span class=\"dates\">累借次数：<strong id=\"loannum2\">" + nownum + "</strong></span>&nbsp;&nbsp;&nbsp;<br /><br /><a href=\"BooksDetail.aspx?BookID=" + bookID + "\" class=\"button button-primary button-small\">详细信息</a><button type=\"button\" class=\"button button-primary button-box button-small btn-success\" style=\"float:right;\" title=\"图书所在位置：\"data-container=\"body\" data-toggle=\"popover\" data-placement=\"bottom\" data-content=\"" + bookplace + "\">+</button><br /><br /><div class=\"palette-paragraph\"><h6 class=\"palette-headline\">本书摘要：</h6>&nbsp;&nbsp;&nbsp;&nbsp;" + bookname + "。		 </div></div></div> </div> </div></li>";


        }
        countNum = drc.Count;//计数
        companynum = sql.EXQSQL("select (PublishCompanyName) INTO #T  from  Book_info,Publish_company,Book_Type where (book_info.PublishCompanyID=Publish_company.PublishCompanyID  and ( BookName like '%" + para + "%' or Writer like '%" + para + "%' ) and  Book_info.BookType=Book_Type.BookType " + para2 + para3 + para4 + " )group by PublishCompanyName select COUNT(*) from #T Drop Table #T");

        writenum = sql.EXQSQL("select (Writer) INTO #T  from  Book_info,Publish_company,Book_Type where (book_info.PublishCompanyID=Publish_company.PublishCompanyID  and ( BookName like '%" + para + "%' or Writer like '%" + para + "%') and Book_info.BookType=Book_Type.BookType " + para2 + para3 + para4 + " )group by Writer select COUNT(*) from #T Drop Table #T");

        publishdatenum = sql.EXQSQL("select (PublishDate) INTO #T  from  Book_info,Publish_company,Book_Type where (book_info.PublishCompanyID=Publish_company.PublishCompanyID  and ( BookName like '%" + para + "%' or Writer like '%" + para + "%' ) and Book_info.BookType=Book_Type.BookType " + para2 + para3 + para4 + " )group by PublishDate select COUNT(*) from #T Drop Table #T");
       // suoyin = companynum + "  " + writenum + "  " + publishdatenum;

        companynum = "<a>出版社 共【"+companynum+"】个</a><ul>";

        DataSet ds2 = sql.getDataset("select  PublishCompanyName,count(PublishCompanyName)from  Book_info,Publish_company,Book_Type where(book_info.PublishCompanyID=Publish_company.PublishCompanyID and ( BookName like '%" + para + "%' or Writer like '%" + para + "%' )  and Book_info.BookType=Book_Type.BookType  " + para2 + para3 + para4 + ") group by PublishCompanyName ", para);
        DataTable dt2 = new DataTable();
        DataRow drow2;
        DataRowCollection drc2;
        dt2 = ds2.Tables[0];
        drc2 = dt2.Rows;

        for (int i = 0; i < drc2.Count; i++)
        {
            drow2 = drc2[i];
            string comname = drow2[0].ToString();
            string num = drow2[1].ToString();
            companynum += "<li><a class=\"active\" href=\"FindBook.aspx?bookName=" + para + "&publishCompany=" + comname + "" + url3 + "" + url4 + "\"><i class=\"icon-chevron-right\"></i>" + comname + "(<span style=\"color:red;\">" + num + " </span>)</a></li>";
        }

        companynum += "</ul></li>";


        writenum=  "<a>作者记录 共【" + writenum + "】个</a><ul>";
        DataSet ds3 = sql.getDataset("select  writer,count(writer)from  Book_info,Publish_company,Book_Type where(book_info.PublishCompanyID=Publish_company.PublishCompanyID and ( BookName like '%" + para + "%' or Writer like '%" + para + "%' )  and Book_info.BookType=Book_Type.BookType  " + para2 + para3 + para4 + ") group by writer ", para);
        DataTable dt3 = new DataTable();
        DataRow drow3;
        DataRowCollection drc3;
        dt3 = ds3.Tables[0];
        drc3 = dt3.Rows;

        for (int i = 0; i < drc3.Count; i++)
        {
            drow3 = drc3[i];
            string comname = drow3[0].ToString();
            string num = drow3[1].ToString();
            writenum += "<li><a class=\"active\" href=\"FindBook.aspx?bookName=" + para + "&writer=" + comname + "" + url3 + "" + url2 + "\"><i class=\"icon-chevron-right\"></i>" + comname + "(<span style=\"color:red;\">" + num + " </span>)</a></li>";
        }

        writenum += "</ul></li>";




        publishdatenum = "<a>出版日期 共【" + publishdatenum + "】个</a><ul>";
        DataSet ds4 = sql.getDataset("select  publishdate,count(publishdate)from  Book_info,Publish_company,Book_Type where(book_info.PublishCompanyID=Publish_company.PublishCompanyID and ( BookName like '%" + para + "%' or Writer like '%" + para + "%' )  and Book_info.BookType=Book_Type.BookType  " + para2 + para3 + para4 + ") group by publishdate ", para);
        DataTable dt4 = new DataTable();
        DataRow drow4;
        DataRowCollection drc4;
        dt4 = ds4.Tables[0];
        drc4 = dt4.Rows;

        for (int i = 0; i < drc4.Count; i++)
        {
            drow4 = drc4[i];
            string comname = drow4[0].ToString();
            string num = drow4[1].ToString();
            publishdatenum += "<li><a class=\"active\" href=\"FindBook.aspx?bookName=" + para + "&publishdate=" + comname + "" + url2 + "" + url4 + "\"><i class=\"icon-chevron-right\"></i>" + string.Format("{0:yyyy-MM}", DateTime.Parse(comname)) + "(<span style=\"color:red;\">" + num + " </span>)</a></li>";
        }

        publishdatenum += "</ul></li>";



    }



    protected void InResult_Click(object sender, EventArgs e)
    {

        string tiaojian = TBRESULT.Text + para2 + para3 + para4;
        para9 = tiaojian;

        setdata2("select distinct BookName,Writer,book_info.PublishCompanyID,PublishDate,ISBN,FindBookNum,Price,Amount,CurrentNum,InDate,BeBorrowTimes,BookPlace,Publish_company.PublishCompanyName,Book_Type.TypeName,BookID  , imageurl from  Book_info,Publish_company,Book_Type where book_info.PublishCompanyID=Publish_company.PublishCompanyID  and ( BookName like '%" + para + "%' or Writer like '%" + para + "%' ) and Book_info.BookType=Book_Type.BookType and ( BookName like '%" + tiaojian + "%' or Writer like '%" + tiaojian + "%' ) " + recordPara + "", para, TBRESULT.Text);
        
    }



    protected void setdata2(string EXSQL, string para,string tiaojian)
    {
        SQL sql = new SQL();
        DataSet ds = sql.getDataset(EXSQL, para);
        DataTable dt = new DataTable();
        DataRow drow;
        DataRowCollection drc;
        dt = ds.Tables[0];
        drc = dt.Rows;
        for (int i = 0; i < drc.Count; i++)
        {
            drow = drc[i];
            string bookname = drow[0].ToString();
            string write = drow[1].ToString();
            string publishdate = drow[3].ToString();
            publishdate = string.Format("{0:yyyy-MM-dd}", DateTime.Parse(publishdate));
            string isbn = drow[4].ToString();
            string findbooknum = drow[5].ToString();
            string price = drow[6].ToString();
            string amount = drow[7].ToString();
            string nownum = drow[8].ToString();
            string indate = string.Format("{0:yyyy-MM-dd}", DateTime.Parse(drow[9].ToString()));
            string beborrow = drow[10].ToString();
            string bookplace = drow[11].ToString();
            string publishCompany = drow[12].ToString();

            string bookTypename = drow[13].ToString();
            string bookID = drow[14].ToString();
            string imageURL = drow[15].ToString();

            data += "<li class=\"span3\" style=\"float: left; text-align: justify; width: 722px;\"><div class=\"thumbnail\"><div class=\"caption\" style=\"overflow:auto\"><div style=\"float:left;width:94px;height:134px;border: 2px solid #cccfd9;\"><img src=\"" + imageURL + "\" ></div><div style=\"float:left;width:575px;margin-left:20px;\"><p style=\"margin-bottom: 5px;padding-bottom: 5px;border-bottom: 1px dashed #b7b7b7;color: #a1973b;font-size: 18px;text-align:left;font-family:FZBeiWeiKaiShu-S19S\"><B>" + bookname + "</B></p><div class=\"titbar\"><span class=\"author\">作者：<strong>" + write + "</strong></span>&nbsp;&nbsp;&nbsp;<span class=\"publisher\">出版社：<strong>" + publishCompany + "</strong></span>&nbsp;&nbsp;&nbsp;<span class=\"dates\">出版时间：<strong>" + publishdate + "</strong></span>&nbsp;&nbsp;&nbsp;<span class=\"dates\">ISBN：<strong>" + isbn + "</strong></span>&nbsp;&nbsp;&nbsp;<br /><br /><span class=\"dates\">索书号：<strong>" + findbooknum + "</strong></span>&nbsp;&nbsp;&nbsp;<span class=\"dates\">入库时间：<strong>" + indate + "</strong></span>&nbsp;&nbsp;&nbsp;<span class=\"dates\">图书总数：<strong>" + amount + "</strong></span>&nbsp;&nbsp;&nbsp;<span class=\"dates\">价格：<strong>" + price + "</strong></span>&nbsp;&nbsp;&nbsp;<br /><br /><span class=\"dates\">图书分类：<strong id=\"hldallnum2\">" + bookTypename + "</strong></span> &nbsp;&nbsp;&nbsp;<span class=\"dates\">在馆数：<strong id=\"hldcount2\">" + nownum + "</strong></span>&nbsp;&nbsp;&nbsp;<span class=\"dates\">累借天数：<strong id=\"loanDatanum2\">" + nownum + "</strong></span>&nbsp;&nbsp;&nbsp; <span class=\"dates\">累借次数：<strong id=\"loannum2\">" + nownum + "</strong></span>&nbsp;&nbsp;&nbsp;<br /><br /><a href=\"BooksDetail.aspx?BookID=" + bookID + "\" class=\"button button-primary button-small\">详细信息</a><button type=\"button\" class=\"button button-primary button-box button-small btn-success\" style=\"float:right;\" title=\"图书所在位置：\"data-container=\"body\" data-toggle=\"popover\" data-placement=\"bottom\" data-content=\"" + bookplace + "\">+</button><br /><br /><div class=\"palette-paragraph\"><h6 class=\"palette-`headline\">本书摘要：</h6>&nbsp;&nbsp;&nbsp;&nbsp;" + bookname + "。		 </div></div></div> </div> </div></li>";


        }
        countNum = drc.Count;//计数
        if (countNum == 0)
        {
            //如果0个结果的话，筛选项也不用显示了，隐藏
            display1 = display2 = display3 = "none";
        }
        companynum = sql.EXQSQL("select (PublishCompanyName) INTO #T  from  Book_info,Publish_company,Book_Type where (book_info.PublishCompanyID=Publish_company.PublishCompanyID  and ( BookName like '%" + para + "%' or Writer like '%" + para + "%' ) and ( BookName like '%" + tiaojian + "%' or Writer like '%" + tiaojian + "%' )  and Book_info.BookType=Book_Type.BookType )group by PublishCompanyName select COUNT(*) from #T Drop Table #T");

        writenum = sql.EXQSQL("select (Writer) INTO #T  from  Book_info,Publish_company,Book_Type where (book_info.PublishCompanyID=Publish_company.PublishCompanyID  and ( BookName like '%" + tiaojian + "%' or Writer like '%" + tiaojian + "%' ) and (BookName like '%" + para + "%' or Writer like '%" + para + "%' ) and Book_info.BookType=Book_Type.BookType )group by Writer select COUNT(*) from #T Drop Table #T");

        publishdatenum = sql.EXQSQL("select (PublishDate) INTO #T  from  Book_info,Publish_company,Book_Type where (book_info.PublishCompanyID=Publish_company.PublishCompanyID  and ( BookName like '%" + para + "%' or Writer like '%" + para + "%' )  and ( BookName like '%" + tiaojian + "%' or Writer like '%" + tiaojian + "%' ) and Book_info.BookType=Book_Type.BookType )group by PublishDate select COUNT(*) from #T Drop Table #T");
        // suoyin = companynum + "  " + writenum + "  " + publishdatenum;

        companynum = "<a>出版社 共【" + companynum + "】个</a><ul>";

        DataSet ds2 = sql.getDataset("select  PublishCompanyName,count(PublishCompanyName)from  Book_info,Publish_company,Book_Type where(book_info.PublishCompanyID=Publish_company.PublishCompanyID and ( BookName like '%" + para + "%' or Writer like '%" + para + "%' )  and ( BookName like '%" + tiaojian + "%' or Writer like '%" + tiaojian + "%' )  and Book_info.BookType=Book_Type.BookType  " + para2 + para3 + para4 + " ) group by PublishCompanyName ", para);
        DataTable dt2 = new DataTable();
        DataRow drow2;
        DataRowCollection drc2;
        dt2 = ds2.Tables[0];
        drc2 = dt2.Rows;

        for (int i = 0; i < drc2.Count; i++)
        {
            drow2 = drc2[i];
            string comname = drow2[0].ToString();
            string num = drow2[1].ToString();
            companynum += "<li><a class=\"active\" href=\"FindBook.aspx?bookName=" + para + "&publishCompany=" + comname + "" + url3 + "" + url4 + "\"><i class=\"icon-chevron-right\"></i>" + comname + "(<span style=\"color:red;\">" + num + " </span>)</a></li>";
        }

        companynum += "</ul></li>";


        writenum = "<a>作者记录 共【" + writenum + "】个</a><ul>";
        DataSet ds3 = sql.getDataset("select  writer,count(writer)from  Book_info,Publish_company,Book_Type where(book_info.PublishCompanyID=Publish_company.PublishCompanyID  and ( BookName like '%" + para + "%' or Writer like '%" + para + "%' ) and ( BookName like '%" + tiaojian + "%' or Writer like '%" + tiaojian + "%' )  and Book_info.BookType=Book_Type.BookType  " + para2 + para3 + para4 + ") group by writer ", para);
        DataTable dt3 = new DataTable();
        DataRow drow3;
        DataRowCollection drc3;
        dt3 = ds3.Tables[0];
        drc3 = dt3.Rows;

        for (int i = 0; i < drc3.Count; i++)
        {
            drow3 = drc3[i];
            string comname = drow3[0].ToString();
            string num = drow3[1].ToString();
            writenum += "<li><a class=\"active\" href=\"FindBook.aspx?bookName=" + para + "&writer=" + comname + "" + url3 + "" + url2 + "\"><i class=\"icon-chevron-right\"></i>" + comname + "(<span style=\"color:red;\">" + num + " </span>)</a></li>";
        }

        writenum += "</ul></li>";




        publishdatenum = "<a>出版日期 共【" + publishdatenum + "】个</a><ul>";
        DataSet ds4 = sql.getDataset("select  publishdate,count(publishdate)from  Book_info,Publish_company,Book_Type where(book_info.PublishCompanyID=Publish_company.PublishCompanyID and ( BookName like '%" + para + "%' or Writer like '%" + para + "%' )  and ( BookName like '%" + tiaojian + "%' or Writer like '%" + tiaojian + "%' ) and Book_info.BookType=Book_Type.BookType  " + para2 + para3 + para4 + ") group by publishdate ", para);
        DataTable dt4 = new DataTable();
        DataRow drow4;
        DataRowCollection drc4;
        dt4 = ds4.Tables[0];
        drc4 = dt4.Rows;

        for (int i = 0; i < drc4.Count; i++)
        {
            drow4 = drc4[i];
            string comname = drow4[0].ToString();
            string num = drow4[1].ToString();
            publishdatenum += "<li><a class=\"active\" href=\"FindBook.aspx?bookName=" + para + "&publishdate=" + comname + "" + url2 + "" + url4 + "\"><i class=\"icon-chevron-right\"></i>" + string.Format("{0:yyyy-MM}", DateTime.Parse(comname)) + "(<span style=\"color:red;\">" + num + " </span>)</a></li>";
        }

        publishdatenum += "</ul></li>";

        para = tiaojian;

    }



    protected void Re_Click(object sender, EventArgs e)
    {
        string bookName = "";
        string url = "";
        bookName = TBRESULT.Text;
        url = "FindBook.aspx?bookName=" + bookName;
        Response.Redirect(url);
    }
}